# -*- coding: utf-8 -*-
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.ext import db

from emof import core as EMOF
from emof import dumper
from emof import loader

from model import Model
import type as lbType

class LoadParameterData(webapp.RequestHandler):
    def get(self):
        m_id = self.request.get("m_id")
        a_id = self.request.get("id")
        model = Model.get(m_id)

        rep = loader.Loader().load(model.model_XMI)
        self.response.headers['Content-Type'] = 'application/xml'
        xml = "<r></r>"
        
        a = rep.findParameter(a_id)
        template_values = {'id': a_id,
                           'p': a}
        path = os.path.join(os.getcwd(), 'xml/Parameter.xml')
        xml = template.render(path, template_values)
                
        self.response.out.write(xml)
                
class SaveParameterData(webapp.RequestHandler):
    def post(self):
        m_id = self.request.get("m_id")
        p_id = self.request.get("id")
        name = self.request.get("name")
        type = self.request.get("type")
        if self.request.get("isOrdered") == '1':
            isOrdered = True
        else:
            isOrdered = False
        if self.request.get("isUnique") == '1':
            isUnique = True
        else:
            isUnique = False
        lower = self.request.get("lower")
        upper = self.request.get("upper")

        model = Model.get(m_id)

        rep = loader.Loader().load(model.model_XMI)
        
        a = rep.findParameter(p_id)
        
        if type:
            t = lbType.getType(type, rep)
            if t:
                a.type = t
        else:
            a.type = None
        
        a.name = name
        a.isOrdered = isOrdered
        a.isUnique = isUnique
        a.lower = lower
        a.upper = upper
        
        model.model_XMI = dumper.Dumper().dump(rep)
        model.put()
                
        self.response.out.write("OK")
        
class CreateParameterData(webapp.RequestHandler):
    def get(self):
        m_id = self.request.get("m_id")
        
        if self.request.get("p_id") != "":
            p_id = self.request.get("p_id")
        else:
            p_id = '-1'
            
        model = Model.get(m_id)

        rep = loader.Loader().load(model.model_XMI)
        fact = EMOF.Factory(rep)       
        a = fact.create_Parameter()
        a.name = 'New Parameter'
        a.isOrdered = False
        a.isUnique = True
        a.lower = 1
        a.upper = 1
        
        operation = rep.findOperation(p_id)

        if operation:
            operation.ownedParameter.append(a)
            a.operation = operation
        model.model_XMI = dumper.Dumper().dump(rep)
        model.put()
                    
        self.response.out.write(a.id + '$$$$' + a.name)
